package cn.edu.fzu.homemaking.chat.controller;

import cn.edu.fzu.homemaking.chat.service.MessageService;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(tags = "CHAT-消息模块")
@RequestMapping("/api/chat")
@RestController
public class MessageController {

    @Resource
    private MessageService messageService;

    @ApiOperation(value = "查询未查看的发送方消息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "from", required = true, value = "发送方id") })
    @GetMapping("/listReceive")
    public Result listReceive(Long from, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute(SessionConstant.USER_ID);
        String userType = (String) session.getAttribute(SessionConstant.USER_TYPE);
        return new Result(messageService.listReceive(userId, from,userType));
    }


    @ApiOperation(value = "查询所有未查看的消息数量（总数量）")
    @GetMapping("/listReceiveCount")
    public Result listReceiveCount(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute(SessionConstant.USER_ID);
        String userType = (String) session.getAttribute(SessionConstant.USER_TYPE);
        return new Result(messageService.listReceiveCount(userId,userType));
    }


    @ApiOperation(value = "查询未查看的消息（发送人员和消息数量）")
    @GetMapping("/listUserCount")
    public Result listUserCount(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute(SessionConstant.USER_ID);
        String origin = (String) session.getAttribute(SessionConstant.USER_TYPE);
        return new Result(messageService.listUserCount(userId, origin));
    }

}
